import { defineStore } from "pinia";
import { useRouter } from 'vue-router';

export const useUserStore = defineStore("user", {
  state: () => ({
    isLoggedIn: false,
    user: null
  }),
  actions: {
    login(userData) {
      this.isLoggedIn = true;
      this.user = userData;
      
      // 模拟保存token到localStorage
      localStorage.setItem('authToken', 'simulated-token');
    },
    logout() {
      this.isLoggedIn = false;
      this.user = null;
      localStorage.removeItem('authToken');
    },
    initialize() {
      // 初始化时检查本地是否有token
      console.log('User state :',this.isLoggedIn);
      const token = localStorage.getItem('authToken');
      if (token) {
        this.isLoggedIn = true;
        // 这里可以添加获取用户信息的逻辑
      }
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'user',
        storage: localStorage
      }
    ]
  }
});